<html>
    <head>
        <title>JSUnit - RosterWindowTest</title>
        <script language="javascript" src="app/jsUnitCore.js"></script>
        
                <script type="text/javascript" src="includes.js"></script>
		<script type="text/javascript" src="common-test-library.js"></script>
        
    </head>
    <body>
        <script type="text/javascript">

var GROUP1 = "Group 1";
var GROUP2 = "Group 2";

var ALIAS1 = "Alias 1";
var ALIAS2 = "Alias 2";

var JID1 = "somebody@somewhere.com";
var JID2 = "somebody2@somewhere.com";

var JID_WITH_RESOURCE = JID1+"/resource1";

var tree;
var pm = null;
var rim = null;

function setUp() {
    pm = new Xmpp4Js.Roster.PresenceManager();
    rim = new Xmpp4Js.Roster.RosterItemManager();
    
    tree = new Xmpp4Js.UI.Roster.BoundRosterTree({
        renderTo: document.body,
        rim: rim,
        pm: pm
    });
}

function tearDown() {
    delete tree;
}

/** 
 * Ensure that item and group are created with first roster item packet.
 * Make sure that it is null before presence but not after.
 */
function testAdd_SingleGroup_() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1] );

    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );

    var group = tree.getGroup( GROUP1 );
    assertNotNull( "Group 1 is null", group );
    
    item = tree.getItem( GROUP1, JID1 );
    assertNotNull( "Item is null after presence", item );
}

/**
 * Ensure that multiple groups are created on first rotser item packet.
 * Make sure that it is null before presence but not after.
 */
function testAdd_TwoGroups() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1, GROUP2] );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( "Group 1 is null but shouldn't be", group );
    
    group = tree.getGroup( GROUP2 );
    assertNotNull( "Group 2 is null but shouldn't be", group );
    
    var item = tree.getItem( GROUP1, JID1 );
    assertNotNull( "Item in Group 1 is null but shouldn't be", item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNotNull( "Item in Group 2 is null but shouldn't be", item );
}

/**
 * Ensure that there are no problems updating an entry in its own group
 * Make sure that it is not null before presence nor after.
 */
function testUpdate_SingleGroup() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1] );
    rim.update( JID1, ALIAS1, "none", null, [GROUP1] );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( "Group 1 is null", group );
    
    var item = tree.getItem( GROUP1, JID1 );
    assertNotNull( "Item is null", item );
}

/**
 * Ensure that there are no problems when an entry changes groups.
 */
function testUpdate_Group1ToGroup2() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1] );
    rim.update( JID1, ALIAS1, "none", null, [GROUP2] );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    // TODO remove group when it's completely empty
    var group = tree.getGroup( GROUP1 );
    assertNotNull( "Group 1 is null", group );
    
    group = tree.getGroup( GROUP2 );
    assertNotNull( "Group 2 is null", group );
    
    // make sure item isn't in group 1
    var item = tree.getItem( GROUP1, JID1 );
    assertNull( "Item from Group 1 IS NOT null but should be", item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNotNull( "Item from Group 2 IS  null but shouldn't be", item );
}

/**
 * Try removing an entry that exists in one gorup, and ensure that the item is gone.
 * NOTE: groups are typically not specified in remove packets.
 */
function testRemove_SingleGroup() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1] );
    rim.update( JID1, ALIAS1, "remove", null, [] );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( group );
    
    var item = tree.getItem( GROUP1, JID1 );
    assertNull( item );
}

/**
 * Remove an entry that exists in multiple groups, and ensure that all items are gone.
 * NOTE: groups are typically not specified in remove packets.
 */
function testRemove_MultupleGroups() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1, GROUP2] );
    rim.update( JID1, ALIAS1, "remove", null, [] );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( group );
    
    var item = tree.getItem( GROUP1, JID1 );
    assertNull( item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNull( item );
}

function testPresence_RosterBeforePresence() {
    rim.update( JID1, ALIAS1, "none", null, [GROUP1, GROUP2] );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( group );
    
    group = tree.getGroup( GROUP2 );
    assertNotNull( group );
    
    var item = tree.getItem( GROUP1, JID1 );
    assertNull( item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNull( item );
    
    item = tree.getItem( Xmpp4Js.UI.Roster.RosterTree.OFFLINE_GROUP, JID1 );
    assertNotNull( item );
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    item = tree.getItem( GROUP1, JID1 );
    assertNotNull( item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNotNull( item );
    
    item = tree.getItem( Xmpp4Js.UI.Roster.RosterTree.OFFLINE_GROUP, JID1 );
    assertNull( item );
}

function testPresence_PresenceBeforeRoster() {
    
    var pres = new Xmpp4Js.Packet.Presence( "available", "local@somewhere.com", null, "normal", null );
    pres.setFrom( JID1+"/resource1" );
    pm.update( pres );
    
    rim.update( JID1, ALIAS1, "none", null, [GROUP1, GROUP2] );
    
    var group = tree.getGroup( GROUP1 );
    assertNotNull( "Expected Group 1 to exist", group );
    
    group = tree.getGroup( GROUP2 );
    assertNotNull( "Expected Group 2 to exist", group );
    
    item = tree.getItem( GROUP1, JID1 );
    assertNotNull( "Expected item to exist in Group 1", item );
    
    item = tree.getItem( GROUP2, JID1 );
    assertNotNull( "Expected item to exist in Group 2", item );
    
    item = tree.getItem( Xmpp4Js.UI.Roster.RosterTree.OFFLINE_GROUP, JID1 );
    assertNull( "Expected item to not exist in offline group", item ); 
}

        </script>
    </body>
</html>




